package com.clusterswisdom.horn.api.ioc.api;

import java.io.InputStream;
import java.util.HashMap;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.clusterswisdom.horn.api.ioc.HornPropertyParser;
import com.clusterswisdom.horn.api.ioc.HornAction;

public class ChannelConfig extends HashMap<String, ApiServiceConfig> {

	private static final long serialVersionUID = -6734238197559408014L;
	
	private String channel;
	
	private HornAction defaultAction;
	
	private HornPropertyParser propertyParser;
	
	public ChannelConfig(String channel) {
		this.channel = channel;
	}
	
	public String getChannel() {
		return channel;
	}
	
	public ApiConfig getApiConfig(String service, String api) {
		ApiServiceConfig serviceConfig = get(service);
		if (serviceConfig == null) {
			return null;
		}
		
		return serviceConfig.getApiConfig(api);
	}

	public ApiServiceConfig getServiceConfig(String service) {
		return get(service);
	}

	public HornAction getDefaultAction() {
		return defaultAction;
	}

	public void setDefaultAction(HornAction defaultAction) {
		this.defaultAction = defaultAction;
	}
	
	public HornPropertyParser getPropertyParser() {
		return propertyParser;
	}

	public void setPropertyParser(HornPropertyParser propertyParser) {
		this.propertyParser = propertyParser;
	}

	public void addServiceConfig(InputStream configIs) throws DocumentException {
		Document document = new SAXReader().read(configIs);
		Element rootElement = document.getRootElement();
		ApiServiceConfig serviceConfig = new ApiServiceConfig(channel, rootElement);
		
		put(serviceConfig.getService(), serviceConfig);
	}
}
